package com.auto.learning.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;

import com.auto.learning.R;
import com.auto.learning.bean.ResponeResult;
import com.auto.learning.bean.UserInfoBean;
import com.auto.learning.event.Handle;
import com.auto.learning.event.listener.DialogListener;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.service.MusicPlayManager;
import com.auto.learning.service.MyUserInfo;
import com.auto.learning.tools.CheckUpdateThread;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.CommonAlertDialog;
import com.auto.learning.view.SharedPreferencesUtils;
import com.auto.learning.view.SwitchButton;

public class FavorSetActivity extends BaseActivity implements OnCheckedChangeListener,OnClickListener {
	
	private SwitchButton button_play_cont,button_download;
	private RelativeLayout rel_loginout,rel_check_version,rel_changepwd,rel_bunding,rel_sleepmode,rel_about;
	
	private Handle handle = new Handle(this){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case HttpRequestHelper.GETDATA_SUCCESS:
				MusicPlayManager.getInstance(FavorSetActivity.this).refreshPlayList();
				ResponeResult result = (ResponeResult) msg.obj;
				try {
					JSONObject jsonObject = new JSONObject(result.getData());
					int userId = jsonObject.getInt(HttpRequestHelper.USERID);
					CommonMethod.setAliasAndTags(FavorSetActivity.this, userId);
					SharedPreferencesUtils.setParam(FavorSetActivity.this, HttpRequestHelper.USERID, userId);
					UserInfoBean userInfoBean = new UserInfoBean();
					userInfoBean.setUserId(userId);
					MyUserInfo.getInstance().setUserInfoBean(userInfoBean);
					setResult(204);
					finish();
				} catch (JSONException e) {
					e.printStackTrace();
				}
				break;
			case 2:
				Log.d("handleMessagexxss", msg.arg1+"--"+msg.arg2 +"--"+ msg.obj);
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setViewInLine(R.layout.favor_activity);
		initView();
		TemporaryVariable.activities.push(this);
	}
	
	@Override
	protected void onDestroy() {
		TemporaryVariable.activities.removeElement(this);
		super.onDestroy();
	}
	
	private void initView(){
		setTitle("设置");
		button_play_cont = (SwitchButton) findViewById(R.id.button_play_cont);
		button_download = (SwitchButton) findViewById(R.id.button_download);
		button_play_cont.setChecked(!(boolean)SharedPreferencesUtils.getParam(this, HttpRequestHelper.CONTINUE_PLAY_BEFORE, true));
		button_download.setChecked(!(boolean)SharedPreferencesUtils.getParam(this, HttpRequestHelper.DWONLOAD_PLAY_CONTINUE, false));
		button_play_cont.setOnCheckedChangeListener(this);
		button_download.setOnCheckedChangeListener(this);
		rel_about = (RelativeLayout) findViewById(R.id.rel_about);
		rel_about.setOnClickListener(this);
		rel_sleepmode = (RelativeLayout) findViewById(R.id.rel_sleepmode);
		rel_sleepmode.setOnClickListener(this);
		rel_loginout = (RelativeLayout) findViewById(R.id.rel_loginout);
		rel_loginout.setOnClickListener(this);
		rel_check_version = (RelativeLayout) findViewById(R.id.rel_check_version);
		rel_check_version.setOnClickListener(this);
		rel_changepwd = (RelativeLayout) findViewById(R.id.rel_changepwd);
		rel_changepwd.setOnClickListener(this);
		rel_bunding = (RelativeLayout) findViewById(R.id.rel_bunding);
		rel_bunding.setOnClickListener(this);
		if (TextUtils.isEmpty(MyUserInfo.getInstance().getUserInfoBean().getSessionId())) {
			rel_loginout.setVisibility(View.GONE);
			rel_bunding.setVisibility(View.GONE);
		}
		backBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (isNeedRefresh) {
					setResult(204);
				}
				finish();
			}
		});
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.button_play_cont:
			SharedPreferencesUtils.setParam(this, HttpRequestHelper.CONTINUE_PLAY_BEFORE, !isChecked);
			break;
		case R.id.button_download:
			SharedPreferencesUtils.setParam(this, HttpRequestHelper.DWONLOAD_PLAY_CONTINUE, !isChecked);
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rel_about:
			Intent about_intent = new Intent(this, AboutusActivity.class);
			startActivity(about_intent);
			break;
		case R.id.rel_loginout:
			final UserInfoBean userInfoBean = MyUserInfo.getInstance().getUserInfoBean();
			if (userInfoBean != null && !TextUtils.isEmpty(userInfoBean.getSessionId())) {
				CommonAlertDialog alertDialog = new CommonAlertDialog(this);
				alertDialog.setTextMessage("确认退出当前账号吗？");
				alertDialog.setShowTitle(true);
				String[] a = {"确定","取消"};
				alertDialog.setBtns(new DialogListener() {

					@Override
					public void okButtonClick(Dialog dialog) {
						HttpRequestHelper.logout(FavorSetActivity.this, userInfoBean.getSessionId(), handle);
						dialog.dismiss();
					}

					@Override
					public void cancelButtonClick(Dialog dialog) {
						dialog.dismiss();
					}

					@Override
					public void otherButtonClick(Dialog dialog) {
						dialog.dismiss();
					}}, a);
				alertDialog.show();
			} else {
				CommonMethod.makeNotice(this, "未登录");
			}
			break;
		case R.id.rel_check_version:
			new CheckUpdateThread(FavorSetActivity.this,0).execute();
			break;
		case R.id.rel_changepwd:
			if (TextUtils.isEmpty(MyUserInfo.getInstance().getUserInfoBean().getSessionId())) {
				CommonMethod.makeNotice(this, "未登录");
				return;
			}
			int type = (int) SharedPreferencesUtils.getParam(this, "loginType", 0);
			if (type == 1) {
				CommonMethod.makeNotice(this, "微信登录没有密码");
				return;
			}else if (type == 2) {
				CommonMethod.makeNotice(this, "QQ登录没有密码");
				return;
			}else if (type == 0) {
				Intent intent = new Intent(this, ChangePasswordActivity.class);
				startActivity(intent);
			}
			break;
		case R.id.rel_bunding:
			Intent intent = new Intent(this, BundingActivity.class);
			startActivityForResult(intent,100);
			break;
		case R.id.rel_sleepmode:
			Intent sleep_intent = new Intent(this, MySleepModeActivity.class);
			startActivity(sleep_intent);
			break;
		}
	}
	boolean isNeedRefresh = false;
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 100 && resultCode == 206) {
			isNeedRefresh = true;
		}
	}
		
}
